/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.controller;

import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIConversion.User;
import cz.cvut.fel.taskpro.business.imanager.IUserBusiness;
import cz.cvut.fel.taskpro.business.support.BusinessRuleException;
import cz.cvut.fel.taskpro.data.entity.Admin;
import cz.cvut.fel.taskpro.data.entity.Kantor;
import cz.cvut.fel.taskpro.data.entity.Student;
import cz.cvut.fel.taskpro.data.entity.TaskProUser;
import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * This controller contains and work with actual logged user. Controller provide
 * actual logged user, upadete this user and logout action for this user.
 * Controller is named as loginBean.
 *
 * @author Tom
 * @version 1.0.0. beta
 */
@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {

    private String password;
    private TaskProUser loggedUser;
    @EJB
    private IUserBusiness userB;

    /**
     * This method logou actual user from application and invalidate all
     * sessions.
     */
    public String logout() {
        FacesContext facescontext = FacesContext.getCurrentInstance();
        HttpServletRequest ref = (HttpServletRequest) facescontext.getExternalContext().getRequest();
        try {
            ref.logout();
            HttpSession session = (HttpSession) facescontext.getExternalContext().getSession(false);
            session.invalidate();
            return "logout";
        } catch (ServletException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            return "logout";
        }
    }

    @PostConstruct
    public void init() {
        recognizeLogin();
    }

    public String recognizeLogin() {
       Principal p = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
       loggedUser=userB.findUserByLogin(p.getName());
        if (loggedUser instanceof Student) {
            return "loginStudent";
        } else if (loggedUser instanceof Kantor) {
            return "loginKantor";
        } else if (loggedUser instanceof Admin) {
            return "loginAdmin";
        } else {
            return "loginStudent";
        }
    }

    /**
     * This method ask data layer for modify actual logged user.
     */
    public void modifyLoggedUser() {
        try {
            userB.modifyUser(loggedUser, password);
            printSuccMessage("User modify.", "Your personal identificators has been successfully modificated.");
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    protected void printErrMessage(int code, String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_FATAL, Integer.toString(code), message));
    }

    protected void printErrMessage(String content, String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_FATAL, content, message));
    }

    protected void printSuccMessage(String content, String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, content, message));
    }

    public TaskProUser getLoggedUser() {
        return loggedUser;
    }

    public void setLoggedUser(TaskProUser loggedUser) {
        this.loggedUser = loggedUser;
    }
    
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
